/*
 * Copyright (C) 2008 nextcode:ch
 * 
 * Author: Michele Dell'Ambrogio, m@nextcode.ch
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as 
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package sfplayer.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import sfplayer.datatypes.Broadcast;

/**
 *
 * @author micc
 */
public class Broadcasts extends JDialog {

    JList list = null;
    DefaultListModel listModel = null;
    WindowMain owner = null;
    
    protected ResourceBundle resbundle;
    
    public Broadcasts(WindowMain owner, List<Broadcast> broadcasts) {
        super(owner, "", false);
        
        resbundle = ResourceBundle.getBundle("sfplayer/resources/broadcasts", Locale.getDefault());
        
        this.setTitle(resbundle.getString("dialogName"));
        
        showList(broadcasts);
        this.owner = owner;
    }
    
    private void showList(List<Broadcast> broadcasts) {
        
        // DefaultListModel
        listModel = new DefaultListModel(); 
        
        for (Broadcast broadcast : broadcasts) {
            if (!broadcast.getMmsUrl().equals("")) {
                BroadcastListItem listObject = new BroadcastListItem(broadcast.getTitle(), broadcast.getMmsUrl());
                listModel.addElement(listObject);
            }
        }
        
        // JList
        list = new JList();
        
        list.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) { // double click

                    BroadcastListItem obj = (BroadcastListItem) list.getSelectedValue();
                    
                    System.out.println("Title: " + obj.getTitle());
                    System.out.println("Url: " + obj.getMmsUrl());
                    
                    owner.openUrl(obj.getMmsUrl());
                    
                }
            }
        });
        
        list.setModel(listModel);
        
        JScrollPane listScroller = new JScrollPane(list);
        listScroller.setPreferredSize(new Dimension(250, 80));
        listScroller.setAlignmentX(LEFT_ALIGNMENT);
        
        JPanel listPane = new JPanel();
        listPane.setLayout(new BoxLayout(listPane, BoxLayout.PAGE_AXIS));
        // listPane.add(Box.createRigidArea(new Dimension(0, 5)));
        listPane.add(listScroller);
        // listPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
        
        Container contentPane = getContentPane();
        contentPane.add(listPane, BorderLayout.CENTER);
        
        pack();

    }

}

